'''
Case Type： 故障&可靠性测试
Case Name： 主机恢复pg_xlog权限，重启数据库成功
Create At: 2020/07/20
@zou_jialiang0505328114
Descption:
步骤 1.启动主备数据库，判断数据库状态；
步骤 2修改主节点pg_xlog权限，查看数据库状态；
步骤 3.运行tpcc；
步骤 4.恢复xlog权限，重启数据库；
'''
import os
import unittest
import time
from yat.test import macro
from yat.test import Node
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

class Deletaduit(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.dbPrimaryRoot = Node(node='PrimaryRoot')
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.log = Logger()
        self.TPCC_PATH = macro.TPCC_PATH
        self.com = Common()
        self.Constant = Constant()

        self.log.info('------------Opengauss_Reliability_Dbsys_Case197.py start------------')
        self.log.info('-----------修改主节点xlog目录权限---------------------------------')
        chmodxlog = 'chmod 000 ' + macro.DB_INSTANCE_PATH + '/pg_xlog'
        self.log.info(chmodxlog)
        self.dbPrimaryDbUser.sh(chmodxlog)

    def test_startdb(self):
        time .sleep(10)
        self.log.info('-----------恢复xlog目录权限，重启数据库---------------------')
        chmodxlog = 'chmod 700 ' + macro.DB_INSTANCE_PATH + '/pg_xlog'
        self.log.info(chmodxlog)
        msg = self.dbPrimaryDbUser.sh(chmodxlog).result()
        self.log.info(msg)

        self.log.info('查询数据库状态')
        status = self.primary_sh.getDbClusterStatus("status")
        self.log.info(status)

        self.log.info('-------------------重启数据库-------------------------------')
        result = self.primary_sh.stopDbCluster()
        self.assertTrue(result)
        result = self.primary_sh.startDbCluster()
        self.assertTrue(result)

        self.log.info('---------------------运行tpcc-------------------------------')
        result = self.com.startTPCC(self.dbPrimaryRoot, self.TPCC_PATH, )
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, result)

    def tearDown(self):
        self.log.info('----------------this is tearDown------------------------------------')
